package com.example.haojiutao.service.impl;

import com.example.haojiutao.common.Result;
import com.example.haojiutao.exception.HaoJiuTaoException;
import com.example.haojiutao.mapper.ReturnOrderMapper;
import com.example.haojiutao.model.vo.response.OrderGoodsDetailVO;
import com.example.haojiutao.model.vo.response.ReOrderGoodsDetailVO;
import com.example.haojiutao.service.ReturnOrderService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: zhou_yuan
 * @CreateTime： 2024/2/5 13:43
 */
@Slf4j
@Service
public class ReturnOrderServiceImpl implements ReturnOrderService {

    @Autowired
    ReturnOrderMapper reorderMapper;
    /**
     * @Description: 退回订单
     * @Author: zhou_yuan
     * @CreateTime： 2024/2/5 13:43
     * @param: order
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public Result insertRetrunOrder(ReOrderGoodsDetailVO reorder) {
        Integer orderNumber= reorder.getOrderNumber();
        int count = reorderMapper.selectCountByOrderNumber(orderNumber);
        if(count>0){
            throw new HaoJiuTaoException("100005","订单已申请售后，请勿重复申请");
        }
        log.info("下单成功");
        reorder.setReturnStatus("待处理");
        reorder.setCreateTime(new Date());
        reorder.setSuccessFlag(0);
        int rows = reorderMapper.insertReturnOrder(reorder);
        if (rows != 0) {
            return Result.success("申请成功！");
        }
        return Result.faile("申请失败");

    }
    /*
     * @Description: 更新收货状态，点击为已收货
     * @Author: zhou_yuan
     * @CreateTime： 2024/2/5 13:44
     * @param: reorder
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public Result updateReturnOrder(ReOrderGoodsDetailVO reorder) {
        OrderGoodsDetailVO order = new OrderGoodsDetailVO();
        log.info("更新订单信息:order{}", reorder);
        reorder.setReturnStatus("已签收");
        reorder.setSuccessFlag(1);//1为成功，0为不成功
        order.setAfterSales(1);
        int rows = reorderMapper.updateReturnOrder(reorder);
        if (rows != 0) {
            return Result.success("修改成功！");

        }
        return Result.faile("修改失败");
    }


    /*
     * @Description: 商家是否同意退货
     * @Author: zhou_yuan
     * @CreateTime： 2024/2/5 13:45
     * @param: reorder
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public Result updateOrderOpinion(ReOrderGoodsDetailVO reorder) {
        log.info("更新订单信息:order{}", reorder);
        reorder.setSellerOpinion("同意");
        int rows = reorderMapper.updateReturnOrder(reorder);
        if (rows != 0) {
            return Result.success("修改成功！");
        }
        return Result.faile("修改失败");
    }


    /**
     * @Description: 添加物流单号
     * @Author: zhou_yuan
     * @CreateTime： 2024/2/5 13:50
     * @param: reorder
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public Result updateOrderLogistics(ReOrderGoodsDetailVO reorder) {
        log.info("更新订单信息:order{}", reorder);
        String retrackingNumber= reorder.getRetrackingNumber();
        if(!StringUtils.hasLength(retrackingNumber)
        ){
            log.warn("用户输入信息为空，retrackingNumber ={} ",retrackingNumber);
            return Result.faile(100001,"快递单号不能为空");
        }
        reorder.setReturnStatus("已发货");
        int rows = reorderMapper.updateReturnOrder(reorder);
        if (rows != 0) {
            return Result.success("修改成功！");
        }
        return Result.faile("修改失败");
    }


    /*
     * @Description: 查询退货订单详情
     * @Author: zhou_yuan
     * @CreateTime： 2024/2/5 13:53
     * @param: orderNumber
     * @return: com.example.haojiutao.model.vo.response.ReOrderGoodsDetailVO
     */
    @Override
    public ReOrderGoodsDetailVO selectByReOrderNumber(Integer reorderId) {
        ReOrderGoodsDetailVO reorder = reorderMapper.selectByReOrderNumber(reorderId);
        log.info("order={}" + reorder);
        return reorder;
    }

    @Override
    public List<OrderGoodsDetailVO> selectListByUser(ReOrderGoodsDetailVO reorder) {
        PageHelper.startPage(reorder.getPageNum(), reorder.getPageSize());
        List<OrderGoodsDetailVO> order1 = reorderMapper.selectListByUser(reorder);
        return order1;
    }

    @Override
    public Result deleteById(Integer reorderId) {
        ReOrderGoodsDetailVO reorder = new ReOrderGoodsDetailVO();
        reorder.setReorderId(reorderId);
        reorder.setDelFlag(1);
//        order.setDelTime(new Date());
        int rows = reorderMapper.updateDeleteFlagById(reorder);
        if (rows == 1) {
            return Result.success("删除成功！");
        }
        return Result.faile("删除失败");
    }
}
